package cn.jdy.framework.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.jdy.framework.util.FileUtil;
import cn.jdy.framework.util.HttpUtil;

/**
 * 内容采集工具类
 * 
 * @author zhaigt
 * @date 2014年11月10日
 */
public class CollectUtil {
  
  /**
   * 获取采集链接 列表
   */
  public static List<String> getLists(String s,String startFlag,String endFlag){
      // 获取标题
//      String title = getTitle(s);
      
      // 去掉指定后缀
//      title = title.replaceAll("_39健康保健_39健康网", "");  
//      System.out.println(title);
      
      // 获取区域内容
      String area = getAreaContent(s,startFlag,endFlag);
      
      // 获取超链接
      return getLink(area);
       
  }
  
  /**
   * 获取指定区域的内容
   * <div class=\"partbox\">(.*?)<div class=\"con_right\">
   */
  public static String getAreaContent(String s,String startFlag,String endFlag) {
      // 获取指定区域的内容
      final Pattern pa = Pattern.compile(startFlag+"(.*?)"+endFlag, Pattern.DOTALL);  
      final Matcher ma = pa.matcher(s);  
      StringBuffer sb = new StringBuffer(); 
      
      while (ma.find()) {  
          sb.append(ma.group(1));
      }  
      return sb.toString();
  }
  
  /**
   * 获得网页标题
   */
  public static String getTitle(final String s) {
      String regex;
      String title = "";
      final List<String> list = new ArrayList<String>();
      regex = "<title>.*?</title>";
      final Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
      final Matcher ma = pa.matcher(s);
      while (ma.find()) {
          list.add(ma.group());
      }
      for (int i = 0; i < list.size(); i++) {
          title = title + list.get(i);
      }
      return outTag(title);
  }
  
  /**
   * @return 获得超链接
   */
  public static List<String> getLink(final String s) {
      String regex;
      final List<String> list = new ArrayList<String>();
      regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";
      final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
      final Matcher ma = pa.matcher(s);
      while (ma.find()) {
          String href = ma.group(2);
          if(href.matches("(http|https)://.*")){
              list.add(href);
          }
//          System.out.println("(2)==>"+ma.group(2));// 链接
//          System.out.println("(5)==>"+ma.group(5));// 标题
      }
      return list;
  }
  
  /**
   * @return 去掉标记
   */
  public static String outTag(final String s) {
      return s.replaceAll("<.*?>", "");
  }
  
  /**
   * @return 获得脚本代码
   */
  public static List<String> getScript(final String s) {
      String regex;
      final List<String> list = new ArrayList<String>();
      regex = "<script.*?</script>";
      final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
      final Matcher ma = pa.matcher(s);
      while (ma.find()) {
          list.add(ma.group());
      }
      return list;
  }
  
  /**
   * @return 获得CSS
   */
  public List<String> getCSS(final String s) {
      String regex;
      final List<String> list = new ArrayList<String>();
      regex = "<style.*?</style>";
      final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
      final Matcher ma = pa.matcher(s);
      while (ma.find()) {
          list.add(ma.group());
      }
      return list;
  }
  
  /**
   * 将html代码转为ubb编码格式
   * */
  public static String html2ubb(String str) {
    str = str.replaceAll("<br[^>]*>", "\n");
    // str = str.replaceAll("<p[^>\/]*\/>/ig,'\n');
    // str = str.replaceAll("\son[\w]{3,16}\s?=\s*([\'\"]).+?\1/ig,'');
    str = str.replaceAll("<p>&nbsp;</p>", "");
    str = str.replaceAll("<hr[^>]*>", "[hr]");
    str = str.replaceAll("<(sub|sup|u|strike|b|i|pre)>", "[$1]");
    str = str.replaceAll("</(sub|sup|u|strike|b|i|pre)>", "[/$1]");
    // str = str.replaceAll("<(\/)?strong>/ig,'[$1b]');
    // str = str.replaceAll("<(\/)?em>/ig,'[$1i]');
    // str = str.replaceAll("<(\/)?blockquote([^>]*)>/ig,'[$1blockquote]');

    // str = str.replaceAll("<img[^>]*smile=\"(\d+)\"[^>]*>/ig,'[s:$1]');
    str = str.replaceAll("<img[^>]*src=\"([^\"]*)\"[^>]*/>", "[img]$1[/img]");
    // str =
    // str.replaceAll("<a[^>]*href=\"([^\"])\")[^>]*>(.+?)</a>","[url=$1]$2[/url]");
    str = str.replaceAll("<a[^>]*>(.+?)</a>", "$1");

    str = str.replaceAll("<[^>]*?>", "");
    str = str.replaceAll("&amp;", "&");
    str = str.replaceAll("&lt;", "<");
    str = str.replaceAll("&gt;", ">");
    str = str.replaceAll("&nbsp;", "");
    str = str.replaceAll("&ldquo;", "“");
    str = str.replaceAll("&rdquo;", "”");
    str = str.replaceAll("&lsquo;", "‘");
    str = str.replaceAll("&rsquo;", "’");
    str = str.replaceAll("&hellip;", "...");
    str = str.replaceAll("&middot;", "·");
    str = str.replaceAll("([\r\n\r\n]{1,})", "\n");

    return str;
  }

  /**
   * 抽取文本中的图片并下载到本地
   * @param text
   * @param access_path 图片访问地址前缀
   * @param location_path 图片存储路径
   * @return
   * @throws Exception
   */
  public String watchImg(String text,String access_path,String location_path) throws Exception {

    // 抓出图片地址 替换图片标示
    String regex = "<img[^>]*src=(\"([^\"]*)\"|\'([^\']*)\')[^>]*>";
    final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
    final Matcher ma = pa.matcher(text);

    while (ma.find()) {
      String imgUrl = ma.group(2);// 图片地址
      if (imgUrl.matches("(http|https)://.*")) {

        // 获取文件格式后缀 组成本地文件保存路径 eg: .jpg
        String suffix = "." + imgUrl.replaceAll("^.*\\.", "");

        // 将匹配到的内容 替换为既定的表现形式 统一在html2ubb()中替换
        // text = text.replace(ma.group(0), "[img]"+imgUrl+"[/img]");
        String path = FileUtil.getFilePath(access_path, suffix);
        text = text.replace(imgUrl, path);

        // list.add(imgSrc);
        // 下载图片到既定位置
        // 启动下载线程异步下载
        HttpUtil.downLoad(imgUrl, location_path + path);
      }
    }
    return text;
  }

  /**
   * 文章内容中需要剔除的代码和标记
   */
  private static final String CONTENT_REX = "(?i)(<iframe([^>]*)>([\\s\\S]*)</iframe>|"
                                            + "<script([^>]*)>([\\s\\S]*)</script>|"
                                            + "<style([^>]*)>([\\s\\S]*)</style>|"
                                            + "<div([^>]*)>|</div>|"
                                            + "<span([^>]*)>|</span>|"
                                            + "<a([^>]*)>|</a>|"
                                            + "<!--([\\s\\S]*)-->)";
  
  /**
   * 采集内容清理，去掉 广告，脚本，样式等
   * @param str
   * @return
   */
  public static String clear(String str){
    return str.replaceAll(CONTENT_REX, "");
  }
}
